package com.testdemo.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.testdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 标题栏
 * 使用id ： titleBar
 */
public class TitleBar extends LinearLayout {


    /**
     * 当按钮被点击
     */
    public interface OnTitleBarClickListener {
        void onBackClick();

        void onRightClick();
    }

    @Bind(R.id.iv_back_small)
    ImageView ivBackSmall;
    @Bind(R.id.tv_left)
    TextView tvLeft;
    @Bind(R.id.ll_back)
    LinearLayout llBack;
    @Bind(R.id.iv_back)
    public ImageView ivBack;
    @Bind(R.id.tv_title)
    public TextView tvTitle;
    @Bind(R.id.iv_right)
    public ImageView ivRight;
    @Bind(R.id.tv_right)
    public TextView tvRight;

    public String leftText;
    public String titleText;
    public String rightText;
    public Drawable rightDrawable;
    public int titleBarBackground;
    public int titleTextColor;
    private boolean showLeftText = false;

    private OnTitleBarClickListener onTitleBarClickListener;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.view_title_bar, this);
        ButterKnife.bind(view);

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBar);
        leftText = a.getString(R.styleable.TitleBar_leftText);
        titleText = a.getString(R.styleable.TitleBar_titleText);
        rightText = a.getString(R.styleable.TitleBar_rightText);
        rightDrawable = a.getDrawable(R.styleable.TitleBar_rightDrawable);
        titleBarBackground = a.getResourceId(R.styleable.TitleBar_titleBarBackground, R.color.transparent);
        titleTextColor = a.getColor(R.styleable.TitleBar_titleTextColor, context.getResources().getColor(R.color.white));
        view.findViewById(R.id.ll_root).setBackgroundResource(titleBarBackground);

        a.recycle();
        initView();
    }


    private void initView() {
        setLeftText(leftText);
        setTitleText(titleText);
        setTitleColor(titleTextColor);


        if (rightDrawable != null) {
            ivRight.setImageDrawable(rightDrawable);
            ivRight.setVisibility(VISIBLE);
            tvRight.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(rightText)) {
            tvRight.setText(rightText);
            tvRight.setVisibility(VISIBLE);
        }
    }

    public void setLeftImageRes(@DrawableRes int leftImageRes) {
        ivBack.setImageResource(leftImageRes);
    }

    public void setLeftText(String titleText) {
        this.leftText = titleText;
        tvLeft.setText(leftText);
        if (TextUtils.isEmpty(leftText)){
            ivBack.setVisibility(VISIBLE);
            ivBackSmall.setVisibility(GONE);
            tvLeft.setVisibility(GONE);
        }else {
            ivBack.setVisibility(GONE);
            ivBackSmall.setVisibility(VISIBLE);
            tvLeft.setVisibility(VISIBLE);
        }
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
        tvTitle.setText(TextUtils.isEmpty(titleText) ? "" : titleText);
    }

    public void setTitleColor(int titleColor) {
        tvTitle.setTextColor(titleColor);
    }

    public void setRightText(String rightText) {
        if (!TextUtils.isEmpty(rightText)) {
            this.rightText = rightText;
            this.rightDrawable = null;
            initView();
        }
    }

    public void setRightDrawable(Drawable rightDrawable) {
        if (rightDrawable != null) {
            this.rightDrawable = rightDrawable;
            this.rightText = null;
            initView();
        }
    }

    public void setOnTitleBarClickListener(OnTitleBarClickListener onTitleBarClickListener) {
        this.onTitleBarClickListener = onTitleBarClickListener;
    }

    @OnClick({R.id.iv_back, R.id.iv_right, R.id.tv_right})
    public void onViewClicked(View view) {
        if (onTitleBarClickListener == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_back:
                onTitleBarClickListener.onBackClick();
                break;
            case R.id.iv_right:
                onTitleBarClickListener.onRightClick();
                break;
            case R.id.tv_right:
                onTitleBarClickListener.onRightClick();
                break;
        }
    }
}
